package jh.com.jhappbf.view;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.text.SimpleDateFormat;

import java.util.Date;

import jh.com.jhappbf.R;


/**
 * 作者：Created by GP on 2018/11/14.
 */
public class ImageDialog extends Dialog {
    public static TextView title;
    public  static ImageView content;
    public  static TextView cancel;
    public  static TextView commit;
    public static TextView plate;
    public static TextView cargoname;
    public static TextView dates;

    private ImageDialog(Context context) {
        super(context);
    }

    public static ImageDialog show(final Context context, final String id, final String plateStr, String cargonameStr, final String fileName, final Context mAppContext) {
        final ImageDialog dialog = new ImageDialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(context, R.layout.dialog_image, null);
        title= (TextView) view.findViewById(R.id.title);
        content= (ImageView) view.findViewById(R.id.content);
        Bitmap bitmap= QRCodeUtil.createQRCodeBitmap(id, 800, 800,"UTF-8","L", "1", Color.BLACK, Color.WHITE,null,0f,null);
        content.setImageBitmap(bitmap);
        cancel= (TextView) view.findViewById(R.id.cancel);
        commit= (TextView) view.findViewById(R.id.commit);
        plate= (TextView) view.findViewById(R.id.plate);
        cargoname= (TextView) view.findViewById(R.id.cargoname);
        dates= (TextView) view.findViewById(R.id.date);
        plate.setText("车牌号："+plateStr);
        cargoname.setText("货  名："+cargonameStr);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime=dateFormat.format(new Date());
        dates.setText("日 期："+dateTime);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View dView = dialog.getWindow().getDecorView();
                dView.setDrawingCacheEnabled(true);
                dView.buildDrawingCache();
                Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
                if(saveImg(context,bitmap,fileName)){
                    dialog.dismiss();
                }
            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              View dView = dialog.getWindow().getDecorView();
              dView.setDrawingCacheEnabled(true);
              dView.buildDrawingCache();
              Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
              if(saveImg(context,bitmap,fileName)){
                  String filePath= Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "qrcode/"+fileName;
                  dialog.dismiss();
              }
            }
        });
        dialog.setContentView(view);
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = display.getWidth() * 9 / 10;
        params.gravity = Gravity.CENTER;
        dialog.getWindow().setAttributes(params);
        dialog.show();
        return dialog;
    }
    public static boolean saveImg(Context context, Bitmap bitmap, String fileName){
        boolean isSaveSuccess = ImageUtil.saveImageToGallery(context, bitmap,fileName);
        if (isSaveSuccess) {
            return true;
        } else {
            return false;
        }
    }
}